using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Google.GData.Client;
using System.Net;
using System.Xml;
using System.Text.RegularExpressions;

namespace puyan_blogger {
    public partial class Plogger : Form {
        private string servicename="blogger";
        private string appname = "PuyanOrg-Plogger-0.1";
        //private string givenname = "Joe Butafuko";

        BlogQuery_cntr blog_query_container = null;
        UserInfo userinfo = null;

        //private Uri bloguri = new Uri("http://blog.puyan.org/feeds/posts/default");
        private Uri list_of_blogs_uri = new Uri("http://www.blogger.com/feeds/default/blogs");


        public Plogger() {
            InitializeComponent();
        }

        private void loginToBlogToolStripMenuItem_Click(object sender, EventArgs e) {
            PloggerPasswordDialog loginDialog = new PloggerPasswordDialog();
            loginDialog.ShowDialog();

            userinfo = new UserInfo(loginDialog.Username, loginDialog.Password);

            blog_query_container =  
                new BlogQuery_cntr(
                    ServiceFactory.ServiceFactoryGenerator(
                        new AppInfo(this.servicename, this.appname),
                        userinfo));
            load_uri();


        }

        private void saveDraftToBlogToolStripMenuItem_Click(object sender, EventArgs e) {
            Uri load_uri = null;
            try {
                load_uri = new Uri(blogUriTextBox.Text);
            } catch (UriFormatException uri_e) {
                return;
            }
            BlogPost_cntr blogpost = new BlogPost_cntr(blogTitleTextBox.Text, blogWriter.Text);
            PloggerAtomUtil.post_content_to_blog(blogpost, blog_query_container, new Uri(blogUriTextBox.Text), true);
        }

        private void postToBlogToolStripMenuItem1_Click(object sender, EventArgs e) {
            Uri load_uri = null;
            try {
                load_uri = new Uri(blogUriTextBox.Text);
            } catch (UriFormatException uri_e) {
                return;
            }
            BlogPost_cntr blogpost = new BlogPost_cntr(blogTitleTextBox.Text, blogWriter.Text);
            PloggerAtomUtil.post_content_to_blog(blogpost, blog_query_container, new Uri(blogUriTextBox.Text), false);
        }



        // FUNCTIONALITY: blog title
        private void blogTitleTextBox_Leave(object sender, EventArgs e) {
            if (blogTitleTextBox.Text.Equals("")) {
                blogTitleTextBox.Text = "Enter Title...";
                blogTitleTextBox.ForeColor = Color.LightGray;
            }
        }
        //private void blogUriTextBox_KeyPress(object sender, KeyPressEventArgs e) {
        //    if (e.KeyChar.Equals('\r')) {
        //        //login_to_blog();
        //    }
        //}
        private void blogTitleTextBox_MouseClick(object sender, MouseEventArgs e) {
            if (!blogTitleTextBox.ForeColor.Equals(Color.Black)) {
                blogTitleTextBox.ForeColor = Color.Black;
                blogTitleTextBox.Text = "";
            }
        }

        private void blogTitleTextBox_KeyPress(object sender, KeyPressEventArgs e) {

        }

        private void blogUriTextBox_MouseClick(object sender, MouseEventArgs e) {
            if (!blogUriTextBox.ForeColor.Equals(Color.Black)) {
                blogUriTextBox.ForeColor = Color.Black;
                blogUriTextBox.Text = "";
            }
        }

        private void blogUriTextBox_Leave(object sender, EventArgs e) {
            if (blogUriTextBox.Text.Equals("")) {
                blogUriTextBox.Text = "Enter Blog Uri...";
                blogUriTextBox.ForeColor = Color.LightGray;
            }
        }

        private void refreshButton_Click(object sender, EventArgs e) {
            load_uri();
        }

        private void saveToFileToolStripMenuItem_Click(object sender, EventArgs e) {
            saveBlogPosttoFileDialog.Filter = "Plain Text Files(*.txt)|*.txt";
            if (saveBlogPosttoFileDialog.ShowDialog() == DialogResult.OK) {
                System.IO.File.WriteAllText(saveBlogPosttoFileDialog.FileName, blogWriter.Text);
                
            }    
        }

        private void loadTextFromFileToolStripMenuItem_Click(object sender, EventArgs e) {
            loadBlogPostfromFileDialog.ShowDialog();

            try {

                blogWriter.Text =
                System.IO.File.
                    ReadAllText(loadBlogPostfromFileDialog.FileName); // TODO: fix exception
            } catch (Exception any_e) {
                return;
            }
            
        }

        private void load_uri() {
            Uri load_uri = null;

            try {
                load_uri = new Uri(blogUriTextBox.Text);
            } catch (UriFormatException uri_e) {
                load_uri = list_of_blogs_uri;
            }

            try {
               
                if(null==blog_query_container) {
                    blog_query_container = new BlogQuery_cntr(new Service());
                }

                PloggerAtomUtil.refresh_rss(blogReaderWebBrowser, blog_query_container, load_uri);
                lock (this) {
                    blogUriTextBox.Text = load_uri.ToString();
                    docTypeLabel.Text = "ATOM";
                }
    
            } catch (Exception bad_atom_feed_e) {
                lock (this) {
                    blogReaderWebBrowser.Url = load_uri;
                    docTypeLabel.Text = "HTML";
                }
            }
        }

        private void blogUriTextBox_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar.Equals('\r')) {
                load_uri();
            }
        }

        private void blogReaderWebBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e) {
            lock (this) {
               if(docTypeLabel.Text.Equals("HTML")) { 
                   blogUriTextBox.Text = e.Url.ToString();
               }
            }
        }

        private void blogReaderWebBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e) {
            blogBrowserProgressBar.Value = (int)(e.CurrentProgress);
        }

        private void toolStripButton3_Click(object sender, EventArgs e) {
            load_uri();
        }

        private void toolStripButton1_Click(object sender, EventArgs e) {
            blogReaderWebBrowser.GoBack();
        }

        private void toolStripButton2_Click(object sender, EventArgs e) {
            blogReaderWebBrowser.GoForward();
        }



   
 

    }
}